
**first import the 2016 dataset 
import delimited "datasets/MPDataset_MPDS2016b_est2.csv", varnames(1) encoding(ISO-8859-1) clear
gen lr_tavits=(per401+per402+per407+per409+per410+per505+per507+per4011+per4012+per4013)- ///
(per403+per404+per406+per412+per413+per503+per504+per506+per4123+per4124+per4131+per4132)


format %24s partyname

keep if countryname=="Albania"|countryname=="Bulgaria"|countryname=="Croatia"| ///
countryname=="Czech Republic"|countryname=="Estonia"|countryname=="Hungary"| ///
countryname=="Latvia"|countryname=="Lithuania"|countryname=="Macedonia"| countryname=="Poland"| ///
countryname=="Romania"|countryname=="Slovakia"|countryname=="Slovenia"

**create average ideology per country/election
*first create party weights based on their seats in parliament* their ideology

gen weighted_id=absseat/totseats*lr_tavits

*create election average
by countryname date, sort: egen election_id=sum(weighted_id)

drop if countryname==countryname[_n-1]&date==date[_n-1]

drop if date>200500

keep countryname edate election_id 

gen election_id2=election_id


split edate, p("/")

gen day=real(edate1)
gen month=real(edate2)
gen year=real(edate3)
gen year2=year

 expand 5
sort countryname year2
by countryname year, sort: replace year2=year2[_n-1]+1 if year2[_n-1]!=.

by countryname, sort: keep if year2<year2[_n+1]
by countryname, sort: keep if year2<year2[_n+1]
by countryname, sort: keep if year2<year2[_n+1]
by countryname, sort: keep if year2<year2[_n+1]
by countryname, sort: keep if year2<year2[_n+1]
by countryname, sort: keep if year2<year2[_n+1]
by countryname, sort: keep if year2<year2[_n+1]

gen date_formated=date(edate, "DMY")

**replace values with averages for the years where there are elections
*first create beginning and end of an year
gen firstday=1
gen lastday=31
gen firstmonth=1
gen lastmonth=12

gen begin_year=mdy(firstmonth, firstday, year2)
gen end_year=mdy(lastmonth, lastday, year2)

**compute the days between the beginning and end of year and the date of elections in election years only
gen firstpart=date_formated-begin_year if year!=year[_n-1]
gen secondpart=end_year-date_formated if year!=year[_n-1]

gen total_days=firstpart+second

*gen proportion of the year to each election
gen first_prop=firstpart/total_days
gen second_prop=secondpart/total_days


by countryname, sort: replace election_id2=second_prop*election_id+first_prop*election_id[_n-1] ///
if year!=year[_n-1]


keep countryname year2 election_id2

*create yearly averages

drop if year>2004
by year, sort: egen avg_parl=mean(election)

drop if year==year[_n-1]

keep year avg

save "figures/avg_parliament.dta", replace
